package com.hoshiicloud.user.service.impl.rbac;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hoshiicloud.common.exception.CommonError;
import com.hoshiicloud.common.exception.ServiceException;
import com.hoshiicloud.rbac.entity.SysDepartmentUsers;
import com.hoshiicloud.user.feign.IdGeneratorService;
import com.hoshiicloud.user.mapper.rbac.SysDepartmentUsersMapper;
import com.hoshiicloud.user.service.rbac.SysDepartmentUsersService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 部门关联用户service接口实现类
 **/
@Service
@Slf4j
public class SysDepartmentUsersServiceImpl extends ServiceImpl<SysDepartmentUsersMapper, SysDepartmentUsers> implements SysDepartmentUsersService {
    private final IdGeneratorService idGeneratorService;

    public SysDepartmentUsersServiceImpl(IdGeneratorService idGeneratorService) {
        this.idGeneratorService = idGeneratorService;
    }

    @Override
    @CacheEvict(cacheNames = "SysDepartmentUsers", allEntries = true)
    public Boolean addSysDepartmentUsers(SysDepartmentUsers entity) {
        // 获取全局id
        String id = idGeneratorService.queryGeneratorId();
        if (StringUtils.isBlank(id)) {
            throw new ServiceException(CommonError.error("id服务失败"));
        }
        entity.setId(Long.valueOf(id));
        int row;
        try {
            row = baseMapper.insert(entity);
        } catch (Exception e) {
            throw new ServiceException("新增部门关联用户失败" + e.getMessage());
        }

        if (row <= 0) {
            throw new ServiceException("新增部门关联用户失败");
        }
        return true;
    }


    @Override
    @CacheEvict(cacheNames = "SysDepartmentUsers", allEntries = true)
    public Boolean deletePhysicByUserId(Long userId) {
        baseMapper.deletePhysicByUserId(userId);
        return true;
    }

    @Override
    @CacheEvict(cacheNames = "SysDepartmentUsers", allEntries = true)
    public Boolean deletePhysicByDepartmentId(Long departmentId) {
        baseMapper.deletePhysicByDepartmentId(departmentId);
        return true;
    }

    @Override
    @Cacheable(cacheNames = "SysDepartmentUsers", key = "#root.targetClass.simpleName+':'+#root.methodName+':'+#departmentId")
    public List<Long> listUsersByDepartmentId(Long departmentId) {
        return this.getLongs(departmentId, null);
    }

    private List<Long> getLongs(Long departmentId, Long userId) {
        QueryWrapper<SysDepartmentUsers> q = new QueryWrapper<>();
        q.lambda().eq((null != departmentId), SysDepartmentUsers::getDepartmentId, departmentId);
        q.lambda().eq((null != userId), SysDepartmentUsers::getUserId, userId);
        List<SysDepartmentUsers> s = baseMapper.selectList(q);
        List<Long> rs = new ArrayList<>();
        if (s.size() > 0) {
            if (null != departmentId) {
                rs = s.stream().map(SysDepartmentUsers::getUserId).collect(Collectors.toList());
            } else if (null != userId) {
                rs = s.stream().map(SysDepartmentUsers::getDepartmentId).collect(Collectors.toList());
            }
        }
        return rs;
    }


    @Override
    @Cacheable(cacheNames = "SysDepartmentUsers", key = "#root.targetClass.simpleName+':'+#root.methodName+':'+#userId")
    public List<Long> listDepartmentsByUserId(Long userId) {
        return this.getLongs(null, userId);
    }


}
